<template>
  <div style="margin: 0 auto; width: 90%">
    <Layout>
      <Header style="font-size: 3em; color: white">土壤风险计算器</Header>
      <Content>
        <div style="margin-top: 0.5rem">
          <RadioGroup v-model="soilType">
            <Radio label="type1">
              <Icon type="logo-apple"></Icon>
              <span>一类用地</span>
            </Radio>
            <Radio label="type2">
              <Icon type="logo-android"></Icon>
              <span>二类用地</span>
            </Radio>
          </RadioGroup>
          <Button type="primary" @click="computeExpose">计算暴露量</Button>
        </div>
        <Tabs value="name1">
          <TabPane label="污染物检测结果" name="soilTableTab"
            ><SoilTable ref="soilTable"></SoilTable
          ></TabPane>
          <TabPane label="污染物毒性参数" name="pollutionTableB1Tab"
            ><PollutionTableB1 ref="pollutionTableB1"></PollutionTableB1
          ></TabPane>
          <TabPane label="污染物理化性质参数" name="pollutionTableB2Tab"
            ><PollutionTableB2 ref="pollutionTableB2"></PollutionTableB2
          ></TabPane>
          <TabPane
            v-if="soilType == 'type1'"
            label="9种摄入途径参数面板（一类用地）"
            name="tab2-1"
            ><SoilType1ExposeParams ref="soilType1ExposeParams"></SoilType1ExposeParams
          ></TabPane>
          <TabPane
            v-if="soilType == 'type2'"
            label="9种摄入途径参数面板（二类用地）"
            name="tab2-2"
            ><SoilType2ExposeParams ref="soilType2ExposeParams"></SoilType2ExposeParams
          ></TabPane>
          <TabPane label="暴露量计算结果" name="tab3">
            <SoilExposeResult ref="soilExposeResult"></SoilExposeResult>
          </TabPane>
          <TabPane label="风险计算结果" name="tab4">风险计算结果</TabPane>
          <TabPane label="控制值计算结果" name="tab5">控制值计算结果</TabPane>
        </Tabs></Content
      >
      <Footer style="text-align: center"
        >1929-2021 &copy; 重庆大学计算机学院</Footer
      >
    </Layout>
  </div>
</template>
<script>
import SoilTable from "@/components/SoilTable.vue";
import PollutionTableB1 from "@/components/PollutionTableB1";
import PollutionTableB2 from "@/components/PollutionTableB2";
import SoilType1ExposeParams from "@/components/SoilType1ExposeParams";
import SoilType2ExposeParams from "@/components/SoilType2ExposeParams";
import SoilExposeResult from "@/components/SoilExposeResult";
export default {
  data() {
    return {
      soilType: "type1",
    };
  },
  components: {
    SoilTable,
    SoilType1ExposeParams,
    SoilType2ExposeParams,
    PollutionTableB1,
    PollutionTableB2,
    SoilExposeResult,
  },
  methods: {
    computeExpose() {
      //从各个面板获取参数
      let soilTable = this.$refs["soilTable"];

      //只计算soilTable中污染物的暴露值，从中找出污染物名称
      var pollutions = [];
      soilTable.data.forEach((element) => {
        pollutions.push(element.name);
      });
      pollutions = Array.from(new Set(pollutions));
      //取污染物对应的b1表
      let b1 = this.$refs["pollutionTableB1"]
      console.log(b1)
      //数组转换为hashmap
      var pollutionsB1 = {};
      b1.data.forEach((e) => {
        if (pollutions.includes(e.name)) pollutionsB1[e.name] = e
      });
      //同理获取对应的b2表
      let b2 = this.$refs["pollutionTableB2"];
      var pollutionsB2 = {};
      b2.data.forEach((e) => {
        if (pollutions.includes(e.name)) pollutionsB2[e.name] = e
      });
      console.log(pollutionsB1);
      console.log(pollutionsB2);
      //取参数表G
      var params;
      if (this.soilType == "type1")
        params = this.$refs["soilType1ExposeParams"]
      else params = this.$refs["soilType2ExposeParams"]
      //计算暴露量，并且设置
      //偷懒临时把函数定义在这里，实际应该弄一个库，或者发到服务器端
      function OISER_ca( ED_c, EF_a, BW_a, OSIR_a, EF_c, ABS_o, AT_ca, BW_c, ED_a, OSIR_c){
          return (((OSIR_c * ED_c * EF_c) / (BW_c) + (OSIR_a * ED_a * EF_a) / (BW_a)) * ABS_o) / (AT_ca) * Math.pow(10,-6)
      }
      function OISER_nc(ED_c, EF_c, BW_c, ABS_o, OSIR_c, AT_nc)
      {
          return (OSIR_c * ED_c * EF_c * ABS_o) / (BW_c * AT_nc) * Math.pow(10,-6)
      }
      function DCSER_ca(SSAR_a, ED_c, SSAR_c, E_v, SAE_a, EF_c, EF_a, BW_a, AT_ca, BW_c, ED_a, ABS_d, SAE_c)
      {
          return (SAE_c * SSAR_c * EF_c * ED_c * E_v * ABS_d) / (BW_c * AT_ca) * Math.pow(10,-6) + (
                SAE_a * SSAR_a * EF_a * ED_a * E_v * ABS_d) / (BW_a * AT_ca) * Math.pow(10,-6)
      }
      function DCSER_nc( ED_c, SSAR_c, E_v, EF_c, BW_c, AT_nc, ABS_d, SAE_c)
      {
          return (SAE_c * SSAR_c * EF_c * ED_c * E_v * ABS_d) / (BW_c * AT_nc) * Math.pow(10,-6)
      }   
      function SAE_c_A4( BW_c,SER_c, H_c)
      {
          return 239 * Math.pow(H_c,0.417)* Math.pow(BW_c,0.517) * SER_c
      }
      function SAE_a_A5( SER_a, BW_a, H_a)
      {
          return 239 * Math.pow(H_a,0.417) * Math.pow(BW_a,0.517) * SER_a
      }
       
      //如果不利用反射api，对参数解包真麻烦
      let ED_c=params.ED_c
      let EF_a=params.ED_a
      let BW_a=params.BW_a
      let OSIR_a=params.OSIR_a
      let EF_c=params.EF_c
      let ABS_o=params.ABS_o
      let AT_ca=params.AT_ca
      let BW_c=params.BW_c
      let ED_a=params.ED_a
      let OSIR_c=params.OSIR_c
      let AT_nc=params.AT_nc
      let SER_c=params.SER_c
      let H_c=params.H_c
      let SER_a=params.SER_a
      let H_a=params.H_a
      let SSAR_a=params.SSAR_a
      let SSAR_c=params.SSAR_c
      let E_v=params.E_v
      var SAE_c=SAE_c_A4( BW_c,SER_c, H_c)
      var SAE_a=SAE_a_A5( SER_a, BW_a, H_a)
      var exposeResult=[]
      pollutions.forEach(e=>{
        console.log(e,pollutionsB1[e])
        var ABS_d=pollutionsB1[e].ABS_d
        
        exposeResult.push({
          name:e,
          OISER_ca:OISER_ca( ED_c, EF_a, BW_a, OSIR_a, EF_c, ABS_o, AT_ca, BW_c, ED_a, OSIR_c),
          OISER_nc:OISER_nc(ED_c, EF_c, BW_c, ABS_o, OSIR_c, AT_nc),
          DCSER_ca:DCSER_ca(SSAR_a, ED_c, SSAR_c, E_v, SAE_a, EF_c, EF_a, BW_a, AT_ca, BW_c, ED_a, ABS_d, SAE_c),
          DCSER_nc:DCSER_nc( ED_c, SSAR_c, E_v, EF_c, BW_c, AT_nc, ABS_d, SAE_c)
        })
      })
      console.log(exposeResult)
      let soilExposeResult=this.$refs["soilExposeResult"]
      soilExposeResult.data=exposeResult


    },
  },
};
</script>